Anze Chen's Blog

ProdMapper V1.0.0

ProdMapper V1.0.0

自动根据源实体类与目标实体类中的注解生成映射类,该类能将源实体类对象转换为目标实体类对象.

###特性:

  • 自动根据注解生成原实体类提高软件可维护性
  • 使用简单连接两个类并转换只需添加 3 行代码
  • 使用基于注解的预编译技术生成的代码速度可媲美手写代码(无性能损耗)
  • 可使用表达式自定义自动生成的映射类的名称
  • (可选)提供使用方便的统一API,使用前无需 Make 项目(使用反射技术有一定性能损耗)
  • 支持源类中的内部类的List对象的嵌套解析
  • 用户友好的编译时错误提示和警告

安装:

在 module 层级的 build.gradle

1
2
3
4
5
6
7
apply plugin: 'com.neenbedankt.android-apt'
...
dependencies {
compile 'com.chenanze:prodmapper:1.0.0'
compile 'com.chenanze:prodmapper-annotation:1.0.0'
compile 'com.chenanze:prodmapper-compiler:1.0.0'
}

使用:

注解待 Map 源类:

在 Mapper 源实体类中的待 Map 类名称上(允许静态内部类)加入 @BindType 注解和 Get 方法以及待生成的Mapper类名称,支持使用表达式指定 Mapper 类名称:

其中$O代表源类名称$T代表目标类名称 (不指定名称则使用默认名称).

正则表达式: ^(.*)(\\$[O|T])(.*)(\\$[O|T])(.*)$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
@BindType(value = Tab.class,proxyClassName = "$O$T")
public static class ProjTabBean {
private String iconFileName;
private String iconFileUrlLink;
private String actionMode;
private String actionParam;
private String id;
public boolean isEnable;
private String name;
private int orderno;
private String project_id;
private String remark;
public ProjTabBean(String iconFileName, String iconFileUrlLink, String actionMode, String actionParam, String id, boolean isEnable, String name, int orderno, String project_id, String remark) {
this.iconFileName = iconFileName;
this.iconFileUrlLink = iconFileUrlLink;
this.actionMode = actionMode;
this.actionParam = actionParam;
this.id = id;
this.isEnable = isEnable;
this.name = name;
this.orderno = orderno;
this.project_id = project_id;
this.remark = remark;
}
public String getIconFileName() {
return iconFileName;
}
public void setIconFileName(String iconFileName) {
this.iconFileName = iconFileName;
}
public String getIconFileUrlLink() {
return iconFileUrlLink;
}
public void setIconFileUrlLink(String iconFileUrlLink) {
this.iconFileUrlLink = iconFileUrlLink;
}
public String getActionMode() {
return actionMode;
}
public void setActionMode(String actionMode) {
this.actionMode = actionMode;
}
public String getActionParam() {
return actionParam;
}
public void setActionParam(String actionParam) {
this.actionParam = actionParam;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
private boolean isIsEnable() {
return isEnable;
}
public void setIsEnable(boolean isEnable) {
this.isEnable = isEnable;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getOrderno() {
return orderno;
}
public void setOrderno(int orderno) {
this.orderno = orderno;
}
public String getProject_id() {
return project_id;
}
public void setProject_id(String project_id) {
this.project_id = project_id;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
@Override
public String toString() {
return "ProjTabBean{" +
"iconFileName='" + iconFileName + '\'' +
", iconFileUrlLink='" + iconFileUrlLink + '\'' +
", actionMode='" + actionMode + '\'' +
", actionParam='" + actionParam + '\'' +
", id='" + id + '\'' +
", isEnable=" + isEnable +
", name='" + name + '\'' +
", orderno=" + orderno +
", project_id='" + project_id + '\'' +
", remark='" + remark + '\'' +
'}';
}
}
@Override
public String toString() {
return "DatasBean{" +
"proj_tab=" + proj_tab +
'}';
}
}

注解待 Map 目标类:

在待 Map 目标类构造器处加入 @Construction注解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
public class Tab extends UIComponent implements Serializable {
private String iconFileName;
private String iconFileUrlLink;
private String actionMode;
private String actionParam;
private boolean isEnable;
private int orderno;
private String project_id;
@Construction
public Tab(String id, String name, String remark, String iconFileName, String iconFileUrlLink, String actionMode, String actionParam, boolean isEnable, int orderno, String project_id) {
super(id, name, remark);
this.iconFileName = iconFileName;
this.iconFileUrlLink = iconFileUrlLink;
this.actionMode = actionMode;
this.actionParam = actionParam;
this.isEnable = isEnable;
this.orderno = orderno;
this.project_id = project_id;
}
public String getIconFileName() {
return iconFileName;
}
public void setIconFileName(String iconFileName) {
this.iconFileName = iconFileName;
}
public String getIconFileUrlLink() {
return iconFileUrlLink;
}
public String getActionMode() {
return actionMode;
}
public void setActionMode(String actionMode) {
this.actionMode = actionMode;
}
public String getActionParam() {
return actionParam;
}
public void setActionParam(String actionParam) {
this.actionParam = actionParam;
}
public boolean isEnable() {
return isEnable;
}
public void setEnable(boolean enable) {
isEnable = enable;
}
public void setIconFileUrlLink(String iconFileUrlLink) {
this.iconFileUrlLink = iconFileUrlLink;
}
public boolean isIsEnable() {
return isEnable;
}
public void setIsEnable(boolean isEnable) {
this.isEnable = isEnable;
}
public int getOrderno() {
return orderno;
}
public void setOrderno(int orderno) {
this.orderno = orderno;
}
public String getProject_id() {
return project_id;
}
public void setProject_id(String project_id) {
this.project_id = project_id;
}
@Override
public String toString() {
return "Tab{" +
"iconFileName='" + iconFileName + '\'' +
", iconFileUrlLink='" + iconFileUrlLink + '\'' +
", actionMode='" + actionMode + '\'' +
", actionParam='" + actionParam + '\'' +
", isEnable=" + isEnable +
", orderno=" + orderno +
", project_id='" + project_id + '\'' +
'}';
}
}

预编译生成 Mapper 类:

请确保源类get 方法名与目标类方法名称在符合驼峰命名规范的前提下一致.

执行make操作,Mapper类将会在路径:

**projectName**/**moduleName**/build/generated/source/apt/debug/com.prodmapper 下生成

使用自动生成的 Mapper 类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
TabEntity tabEntity = new TabEntity(
new ArrayList<DatasBean>() {{
add(new DatasBean(
new ArrayList<DatasBean.ProjTabBean>() {{
add(new DatasBean.ProjTabBean("iconFileName", "iconFileUrlLink", "actionMode", "actionParam", "id", false, "TestName", 0, "0", "remark"));
add(new DatasBean.ProjTabBean("iconFileName1", "iconFileUrlLink1", "actionMode1", "actionParam1", "id1", false, "TestName1", 1, "1", "remark1"));
add(new DatasBean.ProjTabBean("iconFileName2", "iconFileUrlLink2", "actionMode2", "actionParam2", "id2", false, "TestName2", 2, "2", "remark2"));
add(new DatasBean.ProjTabBean("iconFileName3", "iconFileUrlLink3", "actionMode3", "actionParam3", "id3", false, "TestName3", 3, "3", "remark3"));
}}));
add(new DatasBean(
new ArrayList<DatasBean.ProjTabBean>() {{
add(new DatasBean.ProjTabBean("iconFileName4", "iconFileUrlLink4", "actionMode4", "actionParam4", "id4", false, "TestName4", 4, "4", "remark4"));
add(new DatasBean.ProjTabBean("iconFileName5", "iconFileUrlLink5", "actionMode5", "actionParam5", "id5", false, "TestName5", 5, "5", "remark5"));
add(new DatasBean.ProjTabBean("iconFileName6", "iconFileUrlLink6", "actionMode6", "actionParam6", "id6", false, "TestName6", 6, "6", "remark6"));
add(new DatasBean.ProjTabBean("iconFileName7", "iconFileUrlLink7", "actionMode7", "actionParam7", "id7", false, "TestName7", 7, "7", "remark7"));
}}));
add(new DatasBean(
new ArrayList<DatasBean.ProjTabBean>() {{
add(new DatasBean.ProjTabBean("iconFileName8", "iconFileUrlLink8", "actionMode8", "actionParam8", "id8", false, "TestName8", 8, "8", "remark8"));
add(new DatasBean.ProjTabBean("iconFileName9", "iconFileUrlLink9", "actionMode9", "actionParam9", "id9", false, "TestName9", 9, "9", "remark9"));
add(new DatasBean.ProjTabBean("iconFileName10", "iconFileUrlLink10", "actionMode10", "actionParam10", "id10", false, "TestName10", 10, "10", "remark10"));
add(new DatasBean.ProjTabBean("iconFileName11", "iconFileUrlLink11", "actionMode11", "actionParam11", "id11", false, "TestName11", 11, "11", "remark11"));
}}));
}});
// 使用生成的目标类需 make 项目无性能损耗
List<Tab> tabList = ProjTabBeanTab.transform(tabEntity);
// 使用 API 使用前无需 make 项目,但使用反射,有性能损耗
List<Tab> tabList = (List<Tab>) Prodmapper.transfroms(tabEntity, TabEntity.DatasBean.ProjTabBean.class);